<template>
  <div class="myaside-container">
    <div class="toggle-button" @click="changeCollapse">|||</div>
    <el-menu background-color="#333744" text-color="#fff" active-text-color="#409EFF" :unique-opened="true"
      :collapse="isCollapse" :collapse-transition="false" :router="true" :default-active="activePath">
      <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
        <template slot="title">
          <i :class="iconsObj[item.id]"></i>
          <span>{{item.authName}}</span>
        </template>
        <el-menu-item :index="'/'+subItem.path+''" v-for="subItem in item.children" :key="subItem.id"
          @click="saveNavStatus('/'+subItem.path)">
          <template slot="title">
            <i class="el-icon-menu" style="margin-left:-30px"></i>
            <span>{{subItem.authName}}</span>
          </template>
        </el-menu-item>
      </el-submenu>
    </el-menu>
  </div>
</template>
<script>
  export default {
    name: 'MyAside',
    data() {
      return {
        activePath: '',
        isCollapse: false,
        menuList: [],
        iconsObj: {
          '125': 'iconfont icon-user',
          '103': 'iconfont icon-tijikongjian',
          '101': 'iconfont icon-shangpin',
          '102': 'iconfont icon-danju',
          '145': 'iconfont icon-baobiao'
        }
      }
    },
    computed: {},
    created() {
      this.getMenuList()
      this.activePath = window.sessionStorage.getItem('activePath')
    },
    methods: {
      async getMenuList() {
        const {
          data: res
        } = await this.$http.get('menus')
        if (res.meta.status !== 200) {
          return this.$message({
            showClose: true,
            message: '获取菜单失败',
            type: 'error'
          })
        }
        this.menuList = res.data
      },
      changeCollapse() {
        this.isCollapse = !this.isCollapse
      },
      saveNavStatus(path) {
        window.sessionStorage.setItem('activePath', path)
        this.activePath = path
      }
    },
  }

</script>
<style lang="less" scoped>
  .iconfont {
    margin-right: 10px
  }

  .el-menu {
    border-right: none;

    .el-menu-item {
      padding: 0
    }
  }

  .toggle-button {
    background-color: #333744;
    color: white;
    font-size: 10px;
    line-height: 24px;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }

</style>
